package com.education.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.education.realtime.app.BaseAppV1;
import com.education.realtime.bean.AnswerUVBean;
import com.education.realtime.common.Constant;
import com.education.realtime.util.AtguiguUtil;
import com.education.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

public class DwsAnswerUV extends BaseAppV1 {
    public static void main(String[] args) {
        new DwsAnswerUV().init(3157, "DwsAnswerUV", Constant.TOPIC_DWD_EXAM_ISSUE);
    }
    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        // 1. 解析数据, 找到每个用户的第一次答题
        SingleOutputStreamOperator<AnswerUVBean> beanStream = findUv(stream);
        // 2. 开窗聚合
        SingleOutputStreamOperator<AnswerUVBean> aggregatedStream = windowAndAggregate(beanStream);
        // 3. 数据写出到clickhouse中
        writeToClickHouse(aggregatedStream);

    }

    private void writeToClickHouse(SingleOutputStreamOperator<AnswerUVBean> aggregatedStream) {
        aggregatedStream.addSink(FlinkSinkUtil.getClickHouseSink(Constant.CLICKHOUSE_DB,
                "dws_exam_answer_uv_window",
                AnswerUVBean.class
        ));
    }

    private SingleOutputStreamOperator<AnswerUVBean> windowAndAggregate(SingleOutputStreamOperator<AnswerUVBean> beanStream) {
        return  beanStream
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<AnswerUVBean>() {
                            @Override
                            public AnswerUVBean reduce(AnswerUVBean b1,
                                                       AnswerUVBean b2) throws Exception {
                                b1.setUv(b1.getUv()+ b1.getUv());
                                return b1;
                            }
                        },
                        new ProcessAllWindowFunction<AnswerUVBean,AnswerUVBean, TimeWindow>(){
                            @Override
                            public void process(Context ctx,
                                                Iterable<AnswerUVBean> elements,
                                                Collector<AnswerUVBean> out) throws Exception {
                                String stt = AtguiguUtil.toDateTime(ctx.window().getStart());
                                String edt = AtguiguUtil.toDateTime(ctx.window().getEnd());
                                AnswerUVBean bean = elements.iterator().next();
                                bean.setStt(stt);
                                bean.setEdt(edt);
                                bean.setTs(System.currentTimeMillis());
                                out.collect(bean);
                            }
                        }
                );
    }

    private SingleOutputStreamOperator<AnswerUVBean> findUv(DataStreamSource<String> stream) {
       return stream
                .map(JSON::parseObject)
                .assignTimestampsAndWatermarks(WatermarkStrategy
                        .<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((obj,ts)->obj.getLong("ts")*1000)
                )
                .keyBy(obj->obj.getString("user_id"))
                .process(new KeyedProcessFunction<String, JSONObject, AnswerUVBean>() {
                    private ValueState<String> uVstate;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        uVstate = getRuntimeContext().getState(new ValueStateDescriptor<String>("UVstate", String.class));
                    }

                    @Override
                    public void processElement(JSONObject obj,
                                               Context ctx,
                                               Collector<AnswerUVBean> out) throws Exception {
                        long UV=0L;
                        Long ts = obj.getLong("ts");
                        String today = AtguiguUtil.toDate(ts);
                        String is_correct = obj.getString("is_correct");
                        if (is_correct!=null){
                            UV=1L;
                            uVstate.update(today);
                        }
                        if (UV==1){//Uv等于1就代表当天这个用户有一次答对了
                            AnswerUVBean answerUVBean = new AnswerUVBean("","",UV,ts);
                            out.collect(answerUVBean);
                        }
                    }
                });
    }
}
